'''
Created on Mar 2, 2016

@author: JimmyMo
'''
import pymongo
import elasticsearch
import random
import json
import os
from random import random

def insert_log(col, log):
    pass
    
def construct_log(i):
    device_log = {
                  "device_name":"xxxrouter",
                  "ports":[{
                           "CPU": random.randint(1,100),
                           "Memory": random.randint(1, 2048),
                           "Gi3/1/1": {
                                       "inbound": random.randint(100000,200000),
                                       "outbound": random.randint(100000,200000),
                                       "active_con": random.randint(1,1000),
                                       "curreont_con": random.randint(1, 1000),
                                       "total_session": random.randint(1, 10000)
                                       }
                           },
                           {
                            "Gi3/1/2": {
                                        "inbound": random.randint(100000,200000),
                                        "outbound": random.randint(100000,200000),
                                        "metric1": random.randint(1,100),
                                        "active_con": random.randint(1,1000),
                                        "curreont_con": random.randint(1, 1000),
                                        "total_session": random.randint(1, 10000)
                                        }
                            },
                            {
                            "Gi3/1/3": {
                                        "inbound": random.randint(100000,200000),
                                        "outbound": random.randint(100000,200000),
                                        "metric1": random.randint(1,100),
                                        "active_con": random.randint(1,1000),
                                        "curreont_con": random.randint(1, 1000),
                                        "total_session": random.randint(1, 10000)
                                        }
                            },
                            {
                            "Gi3/1/4": {
                                        "inbound": random.randint(100000,200000),
                                        "outbound": random.randint(100000,200000),
                                        "metric1": random.randint(1,100),
                                        "active_con": random.randint(1,1000),
                                        "curreont_con": random.randint(1, 1000),
                                        "total_session": random.randint(1, 10000)
                                        }
                            },
                            {
                            "Gi3/1/5": {
                                        "inbound": random.randint(100000,200000),
                                        "outbound": random.randint(100000,200000),
                                        "metric1": random.randint(1,100),
                                        "active_con": random.randint(1,1000),
                                        "curreont_con": random.randint(1, 1000),
                                        "total_session": random.randint(1, 10000)
                                        }
                            },
                            {
                            "Gi3/1/6": {
                                        "inbound": random.randint(100000,200000),
                                        "outbound": random.randint(100000,200000),
                                        "metric1": random.randint(1,100),
                                        "active_con": random.randint(1,1000),
                                        "curreont_con": random.randint(1, 1000),
                                        "total_session": random.randint(1, 10000)
                                        }
                            }
                           ]
                  
                  }
    one_log = {
               "datetime":"Jan 27 10:35:28",
               "real_ip":"localhost",
               "app":"cft",
               "ip":"76.97.99.97",
               "device_name":"JD76WA01-A2",
               "port":"Gi3/1/1",
               "outbound":random.randint(1000000, 2000000)
               }
    return one_log
def insert_to_mongodb(logs):
    client = pymongo.MongoClient('localhost', 27017)
    db = client.logdb
    collection = db.logcollection
    print('inserting mongo...')
    collection.insert_many(logs)
    print('done...')

def insert_to_elasticsearch(logs):
    es = elasticsearch.Elasticsearch()
    print('inserting elasticsearch...')
    count = 0
    for d in logs:
        print(count)
        es.create(index='sizetest', doc_type='test', body=json.dumps(d))
        count = count + 1
def write_to_file(logs):
#     if not os.path.exists('/tmp/sizetest.log'):
#         os.path
    with open('/tmp/sizetest.log', 'a+') as f:
        for l in logs:
            f.write(str(l))
#         f.writelines('\n'.join(logs))
           
if __name__ == "__main__":
    logs = []
    for i in range(1000000):
        one_log = construct_log(i)
        logs.append(one_log)
    
    write_to_file(logs)
#     insert_to_mongodb(logs)
#     insert_to_elasticsearch(logs)
    
    
#     es.bulk(body, index, doc_type, params)
    